$('.registerBtn').on('click',function(){
	var phone = $('.phoneNum').val();
	if(!(/^(15|13|18|14|17)\d{9}$/).test(phone)){
		$('#errorInfo').html('请填写正确的手机格式');
		$('.mui-active').show();
		$('#errorTip').show();
	}else{
		$.ajax({
			type:"POST",
			url : "/maidian/user/checkPhone.json",
			dataType : "json",
			data : {
				"phone" :phone,
			},
			success : function(data) {
				if(data["code"]=="-200"){
					$('#errorInfo').html('该手机号已经被注册！');
					$('.mui-active').show();
					$('#errorTip').show();
				}else if(data["code"] == "200") {
					$('.tipPhone').html($('.phoneNum').val());
					$('.mui-active').show();
					$('.registerTip').show();
					
				}
			}
		})
	}
})
//注册取消
$('.registerTipCancel').on('click',function(){
	$('.mui-active').hide();
	$('.registerTip').hide();
})


//注册确认
$('.registerTipSubmit').on('click',function(){
	//ajax
	$('.mui-active').hide();
	$('.registerTip').hide();
	var phone = $('.phoneNum').val().trim();
	var sourseType = "r";
	sendCode(phone,sourseType);
	
})

//注册获取短信验证码
$('#sendVerifyCoder').on('click',function(){
	var phone = $('#phone').val().trim();
	var sourseType = "r";
	sendCode(phone,sourseType);
})
//修改获取短信验证码
$('#sendAgin').on('click',function(){
	var phone = $('#phone').val().trim();
	var sourseType = "f";
	if(phone == ''){
		$('#errorInfo').html('请输入手机号码');
		$('.mui-active').show();
		$('#errorTip').show();
	}else if(!(/^(15|13|18|14|17)\d{9}$/).test(phone)){
		$('#errorInfo').html('请输入正确的手机号码');
		$('.mui-active').show();
		$('#errorTip').show();
	}else if(!($(this).attr('disabled') == 'disabled')){
		sendCode(phone,sourseType);	
	}
	
})

//发送验证码
function sendCode(phone,sourseType){
	$.ajax({
		type:"POST",
		url : "/maidian/user/sendVerifyCodeAction.json",
		dataType : "json",
		data : {
			"phone" :phone,
			"sourseType":sourseType,
		},
		success : function(data) {
			if(data["code"] == "1") {
				if(sourseType == "r"){
					window.location.href="/maidian/user/sendVerifyCode.htm?phone="+phone;
				}else{
					var i=59;
					var timer=setInterval(function(){
						$("#sendAgin").html("重发("+i+"s)");	
						$("#sendAgin").attr('disabled','disabled');
						i=i-1;
						if(i<0){
							clearInterval(timer);
							$("#sendAgin").html("获取验证码"); 
							$("#sendAgin").removeAttr('disabled');
						}
						if(i<10){
							i = "0"+i;
						}
					},1000);
				}
				tupianStatus = true;
			}else if(data["code"] == "0") {
				$('#errorInfo').html(data["message"]);
				$('.mui-active').show();
				$('#errorTip').show();
			}else if(data["code"] == "-500") {
				$('#errorInfo').html("手机号码尚未注册");
				$('.mui-active').show();
				$('#errorTip').show();
			}else if(data["code"] == "-201") {
				$('#errorInfo').html("号码发送过于频发");
				$('.mui-active').show();
				$('#errorTip').show();
			}else if(data["code"] == "-400") {
				$('#errorInfo').html("手机号为空");
				$('.mui-active').show();
				$('#errorTip').show();
			}else if(data["code"] == "-600") {
				$('#errorInfo').html("手机号码已注册");
				$('.mui-active').show();
				$('#errorTip').show();
			}
		}
	})
}

//校验短信验证码
$('#toBasicInfo').on('click',function(){
	var phone = $('#phone').val().trim();
	var verifyCode = $('#verifyCode').val().trim();
	if(verifyCode == ''){
		$('#errorInfo').html("请填写验证码");
		$('.mui-active').show();
		$('#errorTip').show();
	}else if(phone == ''){
		$('#errorInfo').html("手机号不存在");
		$('.mui-active').show();
		$('#errorTip').show();
	}else{
		$.ajax({
			type:"POST",
			url : "/maidian/user/toBasicInfo.json",
			dataType : "json",
			data : {
				"phone" :phone,
				"verifyCode":verifyCode
			},
			success : function(data) {
				if(data["responseEntity"]["code"] == "200") {
					window.location.href="/maidian/user/basicInfo.htm?phone="+phone;
				}else if(data["responseEntity"]["code"] == "-200") {
					$('#errorInfo').html("验证码错误，请重新输入");
					$('.mui-active').show();
					$('#errorTip').show();
				}
			}
		})
	}
})

//忘记密码提交
$('#forgetPassword').on('click',function(){
	var phone = $('#phone').val().trim();
	var password = $('#password').val().trim();
	var verifyCode = $('#verifyCode').val().trim();
	if(!(/^(15|13|18|14|17)\d{9}$/).test(phone)){
		$('#errorInfo').html("请填写正确的手机格式");
		$('.mui-active').show();
		$('#errorTip').show();
	}else if(password == '' || verifyCode == '' || phone == ''){
		$('#errorInfo').html("请完善各项信息");
		$('.mui-active').show();
		$('#errorTip').show();
	}else{
		$.ajax({
			type:"POST",
			url : "/maidian/user/forgetPasswordAction.json",
			dataType : "json",
			data : {
				"phone" :phone,
				"password" :password,
				"verifyCode" :verifyCode
			},
			success : function(data) {
				if(data["responseEntity"]["code"] == "200") {
					$('.forgetTip').show();
					$('.mui-active').show();
				}else if(data["responseEntity"]["code"] == "-400") {
					$('#errorInfo').html("验证码输入错误");
					$('.mui-active').show();
					$('#errorTip').show();
				}else if(data["responseEntity"]["code"] == "-300") {
					$('#errorInfo').html("您尚未注册，请先注册");
					$('.mui-active').show();
					$('#errorTip').show();
				}else if(data["responseEntity"]["code"] == "-100") {
					$('#errorInfo').html("请完善各项信息");
					$('.mui-active').show();
					$('#errorTip').show();
				}
			}
		})
	}
})
$('.forgetTipSubmit').on('click',function(){
	$('.forgetTip').hide();
	$('.mui-active').hide();
	window.location.href="/maidian/user/login.htm";
})
//enter键绑定
$(document).keydown(function(event){ 
	event = event || window.event;
	var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
	if(keyCode == 13){ //绑定回车 
		loginAjax();
	} 
}); 

//登录
$('.loginSubmit').on('click',function(){
	loginAjax();
})
function loginAjax(){
	var phone = $('#phone').val().trim();
	var password = $('#password').val().trim();
	if(!(/^(15|13|18|14|17)\d{9}$/).test(phone)){
		$('#errorInfo').html("请填写正确的手机格式");
		$('.mui-active').show();
		$('#errorTip').show();
	}else if(phone == '' || password == ''){
		$('#errorInfo').html("请填写手机号和密码");
		$('.mui-active').show();
		$('#errorTip').show();
	}else{
		$.ajax({
			type:"POST",
			url : "/maidian/user/loginAction.json",
			dataType : "json",
			data : {
				"phone" :phone,
				"password" :password
			},
			success : function(data) {
				if(data["responseEntity"]["code"]=="-200"){
					$('#errorInfo').html("账号或密码错误");
					$('.mui-active').show();
					$('#errorTip').show();
				}else if(data["responseEntity"]["code"] == "200") {
					window.location.href="/maidian/user/index.htm";
				}else if(data["responseEntity"]["code"] == "-300") {
					$('#errorInfo').html("登录失败请重试");
					$('.mui-active').show();
					$('#errorTip').show();
				}
			}
		})
	}
}
//登录
/*$('.loginSubmit').on('click',function(){
	var phone = $('#phone').val().trim();
	var password = $('#password').val().trim();
	if(!(/^(15|13|18|14|17)\d{9}$/).test(phone)){
		$('#errorInfo').html("请填写正确的手机格式");
		$('.mui-active').show();
		$('#errorTip').show();
	}else if(phone == '' || password == ''){
		$('#errorInfo').html("请填写手机号和密码");
		$('.mui-active').show();
		$('#errorTip').show();
	}else{
		$.ajax({
			type:"POST",
			url : "/maidian/user/loginAction.json",
			dataType : "json",
			data : {
				"phone" :phone,
				"password" :password
			},
			success : function(data) {
				if(data["responseEntity"]["code"]=="-200"){
					$('#errorInfo').html("账号或密码错误");
					$('.mui-active').show();
					$('#errorTip').show();
				}else if(data["responseEntity"]["code"] == "200") {
					window.location.href="/maidian/user/index.htm";
				}else if(data["responseEntity"]["code"] == "-300") {
					$('#errorInfo').html("登录失败请重试");
					$('.mui-active').show();
					$('#errorTip').show();
				}
			}
		})
	}
})*/

//友情提示弹框
function tip(error){
	$('#errorInfo').html(error);
	$('.mui-active').show();
	$('#errorTip').show();
}
//友情提示确认
$('.queren').on('click',function(){
	$('.mui-active').hide();
	$('#errorTip').hide();
})
//忘记密码页面
/*$('.forgetPassword').on('click',function(){
	window.location.href="";
})*/